package com.hzit.bj1804.dianweb.controller;

import com.github.pagehelper.PageInfo;
import com.hzit.bj1804.dian.entity.Diaryinfo;
import com.hzit.bj1804.dian.entity.Memberbase;
import com.hzit.bj1804.dian.vo.LayuiTableData;

import com.hzit.bj1804.dianweb.service.DiaryService;
import com.hzit.bj1804.dianweb.service.MemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
/**
 * 会员信息控制器
 */
public class MemberbaseController {
    private int id;
    @Autowired
    MemberService memberbaseService;
    @Autowired
    DiaryService diaryinfoService;
    @RequestMapping("insertMember")
    @ResponseBody
    /**
     * 添加会员的接口
     */
    public Object insert( Memberbase memberbase){
        return memberbaseService.insert(memberbase);
    }
    /**
     * 添加会员的表单页面
     */
    @RequestMapping("insert")
    public  String insert(){
        return "netshop/insert";
    }
    /**
     * 会员跳转页面
     */
    @RequestMapping("seleteAll")
    public String seleteAll(){
        return "netshop/membermanage";
    }
    @RequestMapping("seleteAllData")
    @ResponseBody
    /**
     * 所有会员信息接口
     */
    public Object seleteAllData(){
        LayuiTableData data = new LayuiTableData();
        List<Memberbase> memberbases = memberbaseService.seleteAll();
        for(Memberbase m:memberbases){
            m.setMpassword("");
        }
        data.setData(memberbases);
        data.setCode(0);
        data.setCount(memberbases.size());
        data.setMsg("");
        return data;
    }
    /**
     * 通过时间和会员名查询
     * @return
     */
    @RequestMapping("selectBytime")
    @ResponseBody
     public Object selectBytime(@RequestParam(value = "limit",defaultValue = "5")int limit ,@RequestParam(value = "page",defaultValue = "1")int page,@RequestParam("starttime")String starttime,@RequestParam("endtime")String endtime,@RequestParam("mname")String mname){
        LayuiTableData data = new LayuiTableData();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date start=null;
        Date end=null;
        try {
            start=simpleDateFormat.parse(starttime);
            end=simpleDateFormat.parse(endtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Memberbase> memberbases = memberbaseService.seleteByTime(start, end, mname);
        PageInfo<Memberbase> pageInfo = memberbaseService.seleteByTimePage(limit, page, start, end, mname);
        List<Memberbase> list = pageInfo.getList();
        data.setData(list);
        data.setCode(0);
        data.setCount(memberbases.size());
        data.setMsg("");
        return data;
     }
    /**
     * 根据分页查询所有会员
     */
    @RequestMapping("seleteAllPage")
    @ResponseBody
    public Object seleteAllPage(@RequestParam(value = "limit",defaultValue = "5")int limit ,@RequestParam(value = "page",defaultValue = "1")int page ){
        LayuiTableData data = new LayuiTableData();
        //System.out.println("limit:::"+limit);
      // System.out.println("page:::::"+page);
        List<Memberbase> all = memberbaseService.seleteAll();
        PageInfo<Memberbase> memberbases = memberbaseService.seleteAllPage(limit, page);
       //System.out.println("memberbases::"+memberbases);
        List<Memberbase> list = memberbases.getList();
        data.setData(list);
        data.setCode(0);
        data.setCount(all.size());
        data.setMsg("");
        return data;
    }
    /**
     * 修改会员的接口
     */
    @RequestMapping("update")
    @ResponseBody
    public Object update(@RequestParam("mid") int mid){
        String mstatus="";
        Memberbase memberbase = memberbaseService.selectByMid(mid);
        System.out.println("AA:"+memberbase.getMstatus());
        if(memberbase.getMstatus().equals("1")){
            mstatus="0";
        }
        if(memberbase.getMstatus().equals("0")||memberbase.getMstatus()==null){
            mstatus="1";
        }
        return memberbaseService.update(mid,mstatus);
    }

    /**
     * 登录的接口和登陆日志添加的接口
     * @param mname
     * @param mpassword
     * @param request
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public String  login(@RequestParam("mname")String mname, @RequestParam("mpassword")String mpassword, HttpServletRequest request, HttpSession session){
        Diaryinfo diaryinfo = new Diaryinfo();
        Date date = new Date();
        String addr = request.getRemoteAddr();
        diaryinfo.setLoginname(mname);
        diaryinfo.setIdaddress(addr);
        diaryinfo.setLogintime(date);
        Memberbase select = memberbaseService.select(mname, mpassword);
        session.setAttribute("mname",mname);
        session.setAttribute("mpassword",mpassword);
        //session.setAttribute("select",select);
        if(select!=null){
            diaryinfo.setLoginresult((short)1);
        }else{
            diaryinfo.setLoginresult((short)0);
        }
        diaryinfoService.insert(diaryinfo);
        return select!=null?"0":"1";
    }
    /**
     * 修改密码的接口
     */
    @RequestMapping("updatePassword")
    @ResponseBody
    public Object updatePassword(@RequestParam("mname") String mname,@RequestParam("newmpassword") String newmpassword){
      return   memberbaseService.updatePassword(mname,newmpassword);
    }
    /**
     * 忘记密码的接口
     * @param mname
     * @return
     */
    @RequestMapping("seleteByNmae")
    @ResponseBody
    public String seleteByNmae (@RequestParam("mname") String mname){
        Memberbase memberbase = memberbaseService.seleteByNmae(mname);
        if(memberbase!=null){
            return memberbase.getMpassword();
        }else{
            return "";
        }
    }




    @RequestMapping("membermanage")
    public Object one(){
        return "netshop/membermanage";
    }
    @RequestMapping("in")
    public Object onew(){
        return "netshop/login";
    }
    @RequestMapping("member_report")
    public Object two(){
        return "netshop/member_report";
    }
    @RequestMapping("product_list")
    public Object three(){
        return "netshop/product_list";
    }
    @RequestMapping("itemadd")
    public Object ten(){
        return "netshop/itemadd";
    }
    @RequestMapping("edit_commodity")
    public Object four(){
        return "netshop/edit_commodity";
    }
    @RequestMapping("category_list")
    public Object five(){
        return "netshop/category_list";
    }
    @RequestMapping("order_list")
    public Object six(){
        return "netshop/order_list";
    }
    @RequestMapping("log")
    public Object seven(){
        return "netshop/log";
    }
    @RequestMapping("system_login")
    public Object eight(){
        return "netshop/system_login";
    }
    @RequestMapping("change_password")
    public Object nine(){
        return "netshop/change_password";
    }
    @RequestMapping("moban")
    public Object moban(){
        return "moban";
    }

}
